#include <s5p6818.h>
void delay_ms(unsigned int ms)
{
	unsigned int i,j;
	for(i = 0; i < ms; i++)
		for(j = 0; j < 1800; j++);
}
void hal_led_init(void){//初始化led灯
	GPIOA.ALTFN1 &= (~(0X3 << 24));
	GPIOA.OUTENB |= (0X1 << 28);
	GPIOE.ALTFN0 &= (~(0X3 << 26));
	GPIOE.OUTENB |= (0X1 << 13);
	GPIOB.ALTFN0 &= (~(0X3 << 24));
	GPIOB.ALTFN0 |= (0X1 << 25);
	GPIOB.OUTENB |= (0X1 << 12);
}
void hal_led_red_flash(void){//红灯亮
	GPIOA.OUT |= (0X1 << 28);
}
void hal_led_red_flow(void){//红灯灭
	GPIOA.OUT &= (~(0X1 << 28));
}
void hal_led_greed_flash(void){//绿灯亮
	GPIOE.OUT |= (0X1 << 13);
}
void hal_led_greed_flow(void){//绿灯灭
	GPIOE.OUT &= (~(0X1 << 13));
}
void hal_led_blue_flash(void){//蓝灯亮
	GPIOB.OUT |= (0X1 << 12);
}
void hal_led_blue_flow(void){//蓝灯灭
	GPIOB.OUT &= (~(0X1 << 12));
}
void hal_adc_init(void){//初始化ADC
	IP_RESET_REGISTER1 |= (0X1 << 28);
	ADCCON &= (~(0XF << 10));
	ADCCON &= (~(0XF << 6));
	ADCCON |= (0X6 << 6);
	ADCCON &= (~(0X7 << 3));
	ADCCON &= (~(0X1 << 2));

	PRESCALERCON &= (~(0X3FF));
	PRESCALERCON |= (99);
	PRESCALERCON |= (0X1 << 15);
}
int get_value(void){//ADC取值
	int value;
	ADCCON |= (0X1);
	while((ADCCON & 1));
	value = ADCDAT & 0XFFF;
	value = 2 * value * 1800 / 4095;
	return value;
}
void hal_uart_init(void){//初始化UART
	GPIOD.ALTFN0 &= (~(0X3 << 28));
	GPIOD.ALTFN0 |= (0X1 << 28);
	GPIOD.ALTFN1 &= (~(0X3 << 4));
	GPIOD.ALTFN1 |= (0X1 << 4);
	UART0.ULCON &= (~(0X1 << 6));
	UART0.ULCON &= (~(0X1 << 5));
	UART0.ULCON &= (~(0X1 << 2));
	UART0.ULCON |= (~(0X3));
	UART0.UBRDIV = 27;
	UART0.UFRACVAL = 2;
}
void hal_uar_receive(int value){//接收缓存区=1时有有效数据可以接收数据
	while((UART0.UTRSTAT & 0X1) == 0);
	value = UART0.URXH & 0XFF;
}
void hal_uar_transmit(int ch){//发送缓存区=1时为空可以发送数据
	while((UART0.UTRSTAT & (0X1 << 1)) == 0);
	ch = UART0.URXH & 0XFF;
}
void hal_pwm_init1(void){//2000hz蜂鸣器
	GPIOC.ALTFN0 &= (~(0X3 << 28));
	GPIOC.ALTFN0 |= (0X1 << 29);
	PWM.TCFG0 &= (~(0XFF << 8));
	PWM.TCFG0 |= (249 << 8);
	PWM.TCFG1 &= ((~0XF << 8));
	PWM.TCFG1 |= (0X1 << 8);
	PWM.TCNTB2 = 150;
	PWM.TCMPB2 = 75;
	PWM.TCON |= (0X1 << 14);
	PWM.TCON |= (0X1 << 13);
	PWM.TCON |= (0X1 << 15);
	PWM.TCON &= (~(0X1 << 13));
	PWM.TCON |= (0X1 << 12);
}
//void hal_pwm_open(void){//打开蜂鸣器
//	PWM.TCON |= (0X1 << 12);
//}
void hal_pwm_init2(void){//1000hz蜂鸣器
	GPIOC.ALTFN0 &= (~(0X3 << 28));
	GPIOC.ALTFN0 |= (0X1 << 29);
	PWM.TCFG0 &= (~(0XFF << 8));
	PWM.TCFG0 |= (249 << 8);
	PWM.TCFG1 &= ((~0XF << 8));
	PWM.TCFG1 |= (0X1 << 8);
	PWM.TCNTB2 = 300;
	PWM.TCMPB2 = 150;
	PWM.TCON |= (0X1 << 14);
	PWM.TCON |= (0X1 << 13);
	PWM.TCON |= (0X1 << 15);
	PWM.TCON &= (~(0X1 << 13));
	PWM.TCON |= (0X1 << 12);
}
void hal_pwm_close(void){//关闭蜂鸣器
	PWM.TCON &= (~(0X1 << 12)); 
	GPIOC.ALTFN0 |= (0X3 << 28);
	GPIOC.ALTFN0 &= (~(0X1 << 28));
}
void do_irq(void){//中断执行
	unsigned int irq_num;
	irq_num = GICC_IAR & 0x3FF;
	switch(irq_num){
	case 86:
		if(GPIOB.DET & (1 << 8)){
			printf("key1 interrupt!\n");
			hal_pwm_close();//按下1关闭蜂鸣器	
			GPIOB.DET |= (1 << 8);
		}else if(GPIOB.DET & (1 << 16)){
			printf("key2 interrupt!\n");
			if(get_value() >= 0 && get_value() <= 800){//ADC取值
				printf("key3 interrupt!\n");
				GPIOA.OUT = GPIOA.OUT ^ (1 << 28);//红灯
				GPIOE.OUT = GPIOE.OUT ^ (1 << 13);//绿灯
				GPIOB.OUT = GPIOB.OUT ^ (1 << 12);//蓝灯
			}else if(get_value() <= 1600){
				GPIOE.OUT = GPIOE.OUT ^ (1 << 13);//绿灯
				GPIOB.OUT = GPIOB.OUT ^ (1 << 12);//蓝灯
				printf("key4 interrupt!\n");
			}else if(get_value() <= 2400){
				GPIOA.OUT = GPIOA.OUT ^ (1 << 28);//红灯
				GPIOB.OUT = GPIOB.OUT ^ (1 << 12);//蓝灯
				printf("key5 interrupt!\n");
			}else if(get_value() <= 3300){
				GPIOA.OUT = GPIOA.OUT ^ (1 << 28);//红灯
				GPIOE.OUT = GPIOE.OUT ^ (1 << 13);//绿灯
				printf("key6 interrupt!\n");
			}
			GPIOB.DET |= (1 << 16);
		}
		GICD_ICPENDER.ICPENDER2 |= (1 << 22);
		break;
	case 87:
		break;
	default:
		break;
	}
	GICC_EOIR = (GICC_EOIR & (~0x3FF)) | irq_num;
}
void hal_irq_gpio_init(void){//设置GPIO输入功能和中断触发方式
	GPIOB.ALTFN0 &= (~(0x3 << 16));
	GPIOB.ALTFN1 &= (~(0x3 << 0));
	GPIOB.ALTFN1 |= (0x2 << 0);
	GPIOB.OUTENB &= (~(1 << 8));
	GPIOB.OUTENB &= (~(1 << 16));
	GPIOB.DETMODE0 &= (~(0x3 << 16));
	GPIOB.DETMODE0 |= (2 << 16);
	GPIOB.DETMODEEX &= (~(1 << 8));
	GPIOB.DETMODE1 &= (~(0x3 << 0));
	GPIOB.DETMODE1 |= (2 << 0);
	GPIOB.DETMODEEX &= (~(1 << 16));
	GPIOB.INTENB |= (1 << 8);
	GPIOB.INTENB |= (1 << 16);
	GPIOB.DETENB |= (1 << 8);
	GPIOB.DETENB |= (1 << 16);
}
void hal_irq_gicd_init(void){//设置GICD中断使能寄存器等
	GICD_ISENABLER.ISENABLER2 |= (1 << 22);
	GICD_ITARGETSR.ITARGETSR21 &= (~(0xFF << 16));
	GICD_ITARGETSR.ITARGETSR21 |= (1 << 16);
	GICD_IPRIORITYR.IPRIORITYR21 &= (~(0xFF << 16));
	GICD_IPRIORITYR.IPRIORITYR21 |= (86 << 16);
	GICD_CTRL |= 1;
}
void hal_irq_gicc_init(void){//设置gicc中断使能
	GICC_PMR |= 0xFF;
	GICC_CTRL |= 1;
}
void hal_wdt_init(void){//看门狗初始化
	IP_RESET_REGISTER1 |= (0X1 << 26);
	WTCON &= (~(0XFF << 8));
	WTCON |= (249 << 8);
	WTCON |= (3 << 3);
	WTCNT = 46875;
	WTCON |= (1 << 2);
	WTCON |= (1 << 5);
}
int main()
{
	hal_wdt_init();//看门狗初始化
	hal_irq_gpio_init();//设置GPIO输入功能和中断触发方式
	hal_irq_gicd_init();//设置GICD中断使能寄存器等
	hal_irq_gicc_init();//设置gicc中断使能
	hal_led_init();//初始化led灯
	hal_led_red_flow();//红灯灭
	hal_adc_init();//初始化ADC

	while(1){
		WTCNT = 46875;
		if((get_value() >= 0) && (get_value() <= 800)){//ADC取值
			//	if((PWM.TCON & (0X1 << 12)) == 0 && ((GPIOB.DET & (1 << 8)) == 0)){ 
			//	hal_pwm_init1();//2000hz蜂鸣器
			//	}
			delay_ms(1000);
			printf("%d\n",get_value());
		}else if(get_value() <= 1600){
			//	if((PWM.TCON & (0X1 << 12)) == 1){
			//		hal_pwm_close();//关闭蜂鸣器
			//	}
			if((GPIOA.OUT & (0X1 << 28)) == 0){
				hal_led_greed_flow();//绿灯灭
			}
			hal_led_red_flash();//红灯亮

			delay_ms(1000);
			printf("%d\n",get_value());
		}else if(get_value() <= 2400){
			//	if((PWM.TCON & (0X1 << 12)) == 1){
			//		hal_pwm_close();//关闭蜂鸣器
			//	}
			if((GPIOA.OUT & (0X1 << 13)) == 0){
				hal_led_red_flow();//红灯灭
				hal_led_blue_flow();//蓝灯灭
			}
			hal_led_greed_flash();//绿灯亮
			delay_ms(1000);
			printf("%d\n",get_value());
		}else if(get_value() <= 3300){
			if(get_value() > 3200){
				//	if((PWM.TCON & (0X1 << 12)) == 0 && ((GPIOB.DET & (1 << 8)) == 0)){ 
				//if((PWM.TCON & (0X1 << 12)) == 0){ 
				//			PWM.TCON &= (~(0X1 << 15));
				//			PWM.TCON &= (~(0X1 << 14));
				//			hal_pwm_init2();//1000hz蜂鸣器
				//		}
			}
			if((GPIOA.OUT & (0X1 << 12)) == 0){
				hal_led_greed_flow();//绿灯灭
			}
			hal_led_blue_flash();//蓝灯亮
			delay_ms(2000);
			printf("%d\n",get_value());
			}
		}
		return 0;
	}
